package org.scboy.dbms.controller;

import java.io.IOException;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.InterFrontService;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.ServletResponse;

@RestController
@RequestMapping("/base/interfront")
@Menu("微服务管理系统.语言前端配置")
public class InterFrontController {
	
	@Autowired
	InterFrontService  service;
	
	@PutMapping("update")
	@Permission("更新配置")
	public Result update(@RequestBody Model params){
		if(service.update(params)){
			return Result.Success("INTER_LANG_UPDATE_SUCCESS");
		}else{
			return Result.Error("INTER_LANG_UPDATE_ERROR");
		}		
	}

	@GetMapping("get")
	@Permission("获取配置信息")
	public Result get(String lang){
		return Result.Success(service.get(lang));
	}
	
	/**
	 * 获取国际化语言配置数据，不需要权限验证
	 * @return
	 */
	@GetMapping("list")
	public Result list(){
		return Result.Success(service.list());
	}
	

	/**
	 * 获取国际化语言配置数据，不需要权限验证
	 * @return
	 * @throws IOException 
	 */
	@GetMapping("lang/{lang}.ts")
	public void list(@PathVariable(value = "lang") String lang,ServletResponse response) throws IOException{
		String config=service.lang(lang);
		response.setContentType("application/javascript");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(config);
	}
}
